    //
//  PropertyAnimationController.m
//  AnimationStudy
//
//  Created by longhuihu on 11-6-24.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PropertyAnimationController.h"

@interface DemoViewBase : UIControl
{
		UIImageView* faceView;
}
- (IBAction)actionStartAnimation:(id)sender;
- (void)startAnimation;
@end

@implementation DemoViewBase

-(id)initWithFrame:(CGRect)frame
{
	if (self=[super initWithFrame:frame]) {
		[self addTarget:self action:@selector(actionStartAnimation:) forControlEvents:UIControlEventTouchUpInside];
		faceView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
		[faceView setImage:[UIImage imageNamed:@"face.png"]];
		[self addSubview:faceView];
		[faceView release];
	}
	return self;
}

-(void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	const CGFloat color[] = {1,1,1,1.0};
	const CGFloat linieColor[] = {0,0.5,1,1.0};
	CGContextSetFillColor(context, color);
	CGContextSetStrokeColor(context, linieColor);
	CGContextSetLineWidth(context, 2);
	CGContextFillRect(context, self.bounds);
	CGContextStrokeRect(context, CGRectMake(1, 1, self.bounds.size.width-2, self.bounds.size.height-2));
}

-(void)actionStartAnimation:(id)sender
{
	[self startAnimation];
}
- (void)startAnimation
{
}
@end


@interface SimpleMove : DemoViewBase
@end

@implementation SimpleMove
- (void)startAnimation
{
	[UIView animateWithDuration:1.0 
						  delay:0
						options:0
					 animations:^(void) {
						 faceView.frame = CGRectMake(90, 90, 60,60);
					 }
					 completion:^(BOOL finished) {
						 [UIView animateWithDuration:1.0 
										  animations:^(void){
											  faceView.frame = CGRectMake(10, 10, 50, 50);
										  }];
					 }
	 ];
}
@end

@interface HeartBeat : DemoViewBase
@end

@implementation HeartBeat
- (void)startAnimation
{
	[UIView animateWithDuration:0.5
						  delay:0
						options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse
					 animations:^(void) {
						 [UIView setAnimationRepeatCount:5.5];
						 faceView.frame = CGRectMake(5, 5, 60, 60);
					 }
					 completion:^(BOOL finished) {
						 [UIView animateWithDuration:0.5 
										  animations:^(void){
											  faceView.frame = CGRectMake(10, 10, 50, 50);
										  }];
					 }
	 ];
}
@end

@implementation PropertyAnimationController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	SimpleMove *view = [[SimpleMove alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
	[self.view addSubview:view];
	[view release];
	
	HeartBeat *heatBeat = [[HeartBeat alloc] initWithFrame:CGRectMake(160, 0, 150, 150)];
	[self.view addSubview:heatBeat];
	[heatBeat release];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
